<template>
  
  <section class="todoapp">
    
    <!-- 除了驼峰, 还可以使用-转换链接 -->
  <TodoHeader @addFn="addFn" :list="list">  </TodoHeader>    
    <TodoMain :list="showList" @delFn="delFn">  </TodoMain>    
    <TodoFooter
      :farr="list.length"
      @clearFn="clearFn"
      @changeType="typeFn"
      :type="getSel"
    ></TodoFooter>
    
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css";
import "./styles/index.css";

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem("list")) || [],
      getSel: "all",
    };
  },
  computed: {
    showList() {
      if (this.getSel === "yes") {
        return this.list.filter((item) => item.isDone);
      } else if (this.getSel === "no") {
        return this.list.filter((item) => !item.isDone);
      } else {
        return this.list;
      }
    },
  },
  methods: {
    //添加
    addFn(task) {
      // console.log('按下了enter');
      let id =
        this.list.length === 0 ? 100 : this.list[this.list.length - 1].id + 1;
      this.list.push({
        id,
        name: task,
        isDone: false,
      });
    }, //删除
    delFn(id) {
      // console.log('删除');
      // let index = this.list.findIndex(item => item.id === id)
      // this.list.splice(index,1)
      this.list = this.list.filter((item) => item.id !== id);
    }, //清空已完成
    clearFn() {
      this.list = this.list.filter((item) => !item.isDone);
    },
    typeFn(type) {
      this.getSel = type;
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("list", JSON.stringify(this.list));
      },
    },
  },
};
</script>
